package com.iseastar.guojiang.task;

import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iseastar.dianxiaosan.model.Const;
import com.iseastar.guojiang.model.ServiceTaskBean;
import com.shouhuobao.station.R;
import com.shouhuobao.take.utils.StringUtils;

import java.util.ArrayList;

import droid.frame.activity.base.BaseAdapterExt;
import droid.frame.utils.sqlite.DateUtils;

/**
 * 任务历史列表
 * Created by lidandan on 2018/1/18.
 */
public class TaskHistoryAdapter extends BaseAdapterExt<ServiceTaskBean> {

    public TaskHistoryAdapter(ArrayList<ServiceTaskBean> items, Activity context) {
        super(items, context);
        setEmptyView(R.layout.wallet_money_record_empty);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (hasEmptyView()) {
            return super.getView(position, convertView, parent);
        }
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.wallet_history_task_item, null);
            holder.taskCodeTV = (TextView) convertView.findViewById(R.id.task_code_tv);
            holder.completeOrCancelTV = (TextView) convertView.findViewById(R.id.complete_or_cancel);
            holder.timeTV = (TextView) convertView.findViewById(R.id.time_tv);
            holder.addressTV = (TextView) convertView.findViewById(R.id.address_tv);
            holder.typeTV = (TextView) convertView.findViewById(R.id.type_tv);
            holder.moneyTV = (TextView) convertView.findViewById(R.id.money_tv);
            holder.sameCityLayout = (LinearLayout) convertView.findViewById(R.id.same_city_ll);
            holder.takeTV = (TextView) convertView.findViewById(R.id.take_tv);
            holder.sendTV = (TextView) convertView.findViewById(R.id.send_tv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ServiceTaskBean item = items.get(position);
        holder.taskCodeTV.setText("任务编号：" + item.getTaskNo());
        if (item.getStatus() == Const.CourierTaskDoingStatus.TaskFinished) {
            holder.completeOrCancelTV.setText("已完成");
        } else if (item.getStatus() == Const.CourierTaskDoingStatus.TaskCancel) {
            holder.completeOrCancelTV.setText("支付超时已取消");
        } else if (item.getStatus() == Const.CourierTaskDoingStatus.TaskException) {
            holder.completeOrCancelTV.setText("任务异常");
        } else if (item.getStatus() == Const.CourierTaskDoingStatus.TaskCanelForUser) {
            holder.completeOrCancelTV.setText("用户取消");
        } else if (item.getStatus() == Const.CourierTaskDoingStatus.TaskCanelForCourier) {
            holder.completeOrCancelTV.setText("火箭侠取消");
        }
        holder.timeTV.setText(DateUtils.format(item.getTaskPostTime()));
        //设置地址的布局
        if (item.getTaskType() == Const.CourierTaskStatus.sameCityTask) {
            holder.addressTV.setVisibility(View.GONE);
            holder.sameCityLayout.setVisibility(View.VISIBLE);
            holder.typeTV.setTextColor(Color.parseColor("#0F0F0F"));
            if (StringUtils.isEmpty(item.getAddress())) {
                holder.takeTV.setText("取：" + item.getBuildName());
            } else {
                holder.takeTV.setText("取：" + item.getBuildName() + "（" + item.getAddress() + "）");
            }
            if (StringUtils.isEmpty(item.getGiveAddress())) {
                holder.sendTV.setText("送：" + item.getGiveBuildName());
            } else {
                holder.sendTV.setText("送：" + item.getGiveBuildName() + "（" + item.getGiveAddress() + "）");
            }
        } else {
            holder.addressTV.setVisibility(View.VISIBLE);
            holder.sameCityLayout.setVisibility(View.GONE);
            if (StringUtils.isEmpty(item.getAddress())) {
                holder.addressTV.setText(item.getBuildName());
            } else {
                holder.addressTV.setText(item.getBuildName() + "（" + item.getAddress() + "）");
            }
            holder.typeTV.setTextColor(context.getResources().getColor(R.color.white));
        }
        //回仓
        if (item.getTaskType() == Const.CourierTaskStatus.transferTask) {
            holder.moneyTV.setText(item.getBagCount() + "包裹");
            holder.typeTV.setText("包裹回仓");
            holder.typeTV.setBackgroundResource(R.drawable.parcel_to_step_5c7cd8_bg);
        } else {
            holder.moneyTV.setText("￥" + item.getTaskLucre());
            //上门
            if (item.getTaskType() == Const.CourierTaskStatus.visitTask) {
                holder.typeTV.setText("上门取件");
                holder.typeTV.setBackgroundResource(R.drawable.parcel_to_step_bg);
            }
            //驿站
            else if (item.getTaskType() == Const.CourierTaskStatus.stationTask) {
                holder.typeTV.setText("驿站揽收");
                holder.typeTV.setBackgroundResource(R.drawable.parcel_to_step_bg);
            }
            //同城
            else if (item.getTaskType() == Const.CourierTaskStatus.sameCityTask) {
                holder.typeTV.setText("同城直送");
                holder.typeTV.setBackgroundResource(R.drawable.samecity_task_type_bg);
            }
            //快递柜
            else if (item.getTaskType() == Const.CourierTaskStatus.cabinetTask) {
                holder.typeTV.setText("快递柜取件");
                holder.typeTV.setBackgroundResource(R.drawable.parcel_to_step_bg);
            }
        }

        return convertView;
    }

    class ViewHolder {
        private TextView taskCodeTV;
        private TextView completeOrCancelTV;
        private TextView timeTV;
        private TextView addressTV;
        private TextView typeTV;
        private TextView moneyTV;
        private LinearLayout sameCityLayout;
        private TextView takeTV;
        private TextView sendTV;
    }
}
